package com.xianrenit.xianrenpuzi.order.controller;

import com.xianrenit.xianrenpuzi.model.dto.h5.OrderSubmitDto;
import com.xianrenit.xianrenpuzi.model.entity.order.OrderInfo;
import com.xianrenit.xianrenpuzi.model.result.Result;
import com.xianrenit.xianrenpuzi.model.vo.h5.TradeVo;
import com.xianrenit.xianrenpuzi.order.service.OrderService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/order/orderInfo")
@Tag(name = "订单模块")
public class OrderController {
    @Resource
    OrderService orderService;
    @Operation(summary = "支付成功更新订单支付信息")
    @PutMapping("/auth/updateOrderInfo/{orderNo}")
    public Result updateOrderInfo(@PathVariable("orderNo")String orderNo){
        orderService.updateOrderInfo(orderNo);
        return Result.ok();
    }
    @Operation(summary = "查询用户的订单列表")
    @GetMapping("/auth/{pageNum}/{pageSize}")
    public Result orderPage(@PathVariable("pageNum")Integer pageNum,
                            @PathVariable("pageSize")Integer pageSize,
                            Integer orderStatus){
        PageInfo<OrderInfo> page =   orderService.getOrderPage(pageNum,pageSize,orderStatus);
        return Result.ok(page);
    }

    @Operation(summary = "立即购买")
    @GetMapping("auth/buy/{skuId}")
    public Result buy(@PathVariable("skuId")Long skuId){
        TradeVo tradeVo = orderService.buy(skuId);
        return Result.ok(tradeVo);
    }
    @Operation(summary = "根据订单id查询订单")
    @GetMapping("auth/{id}")
    public Result getOrderById(@PathVariable("id")Long id){
        OrderInfo orderInfo = orderService.getById(id);
        return Result.ok(orderInfo);
    }
    @Operation(summary = "根据订单编号查询订单")
    @GetMapping("auth/getOrderByNo/{orderNo}")
    public Result getOrderByNo(@PathVariable("orderNo")Long orderNo){
        OrderInfo orderInfo = orderService.getOne(Wrappers.lambdaQuery(OrderInfo.class)
                .eq(OrderInfo::getOrderNo , orderNo));
        return Result.ok(orderInfo);
    }
    @Operation(summary = "结算页提交订单接口：保存订单信息")
    @PostMapping("auth/submitOrder")
    public Result submitOrder(@RequestBody OrderSubmitDto orderSubmitDto){
        Long orderId = orderService.submitOrder(orderSubmitDto);
        return Result.ok(orderId);
    }
    //查询购物车中选中的购物项列表 转为orderItem列表交给前端展示
    @Operation(summary = "去结算接口：购物车页点击结算按钮")
    @GetMapping("/auth/trade")
    public Result trade(){
        TradeVo tradeVo = orderService.trade();
        return Result.ok(tradeVo);
    }
}
